package cn.iocoder.yudao.module.wine.controller.admin.advertisement;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wine.controller.admin.advertisement.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.advertisement.AdvertisementDO;
import cn.iocoder.yudao.module.wine.service.advertisement.AdvertisementService;

@Tag(name = "管理后台 - 广告信息")
@RestController
@RequestMapping("/wine/advertisement")
@Validated
public class AdvertisementController {

    @Resource
    private AdvertisementService advertisementService;

    @PostMapping("/create")
    @Operation(summary = "创建广告信息")
    @PreAuthorize("@ss.hasPermission('wine:advertisement:create')")
    public CommonResult<Long> createAdvertisement(@Valid @RequestBody AdvertisementSaveReqVO createReqVO) {
        return success(advertisementService.createAdvertisement(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新广告信息")
    @PreAuthorize("@ss.hasPermission('wine:advertisement:update')")
    public CommonResult<Boolean> updateAdvertisement(@Valid @RequestBody AdvertisementSaveReqVO updateReqVO) {
        advertisementService.updateAdvertisement(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除广告信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wine:advertisement:delete')")
    public CommonResult<Boolean> deleteAdvertisement(@RequestParam("id") Long id) {
        advertisementService.deleteAdvertisement(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得广告信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wine:advertisement:query')")
    public CommonResult<AdvertisementRespVO> getAdvertisement(@RequestParam("id") Long id) {
        AdvertisementDO advertisement = advertisementService.getAdvertisement(id);
        return success(BeanUtils.toBean(advertisement, AdvertisementRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得广告信息分页")
    @PreAuthorize("@ss.hasPermission('wine:advertisement:query')")
    public CommonResult<PageResult<AdvertisementRespVO>> getAdvertisementPage(@Valid AdvertisementPageReqVO pageReqVO) {
        PageResult<AdvertisementDO> pageResult = advertisementService.getAdvertisementPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AdvertisementRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出广告信息 Excel")
    @PreAuthorize("@ss.hasPermission('wine:advertisement:export')")
    @OperateLog(type = EXPORT)
    public void exportAdvertisementExcel(@Valid AdvertisementPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AdvertisementDO> list = advertisementService.getAdvertisementPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "广告信息.xls", "数据", AdvertisementRespVO.class,
                        BeanUtils.toBean(list, AdvertisementRespVO.class));
    }

}